home *** CD-ROM | disk | FTP | other *** search
- /*------------------------------------------------------------------*
- * FILE: PSP.C *
- * DESC: Program to print text files on PostScript printers. *
- * AUTHOR: Marv Luse, Ithaca Street Software, Inc. *
- * DATE: December, 1989 *
- * COPYRIGHT: Use freely as long as authorship is acknowledged. *
- *------------------------------------------------------------------*/
-
- #include "stdlib.h"
- #include "stdio.h"
- #include "string.h"
- #include "time.h"
-
- /*
- * Notes: (1) Program creates an 8-1/2" x 11" page with 3/4"
- * margins all around. Top 1/4" and bottom 1/4"
- * of active page area used for header and footer.
- *
- * (2) All page coords are in points (72 points = 1 inch)
- *
- * (3) Page origin (0,0) located at bottom left, yielding:
- *
- * +--------------------------------------+
- * | |
- * | + (54,738) (558,738) + |
- * | |
- * | |
- * / /
- * : Page Coordinate Space :
- * / in Points /
- * | |
- * | |
- * | |
- * | + (54,54) (558,54) + |
- * | |
- * +--------------------------------------+
- *
- * (4) Control char handling:
- *
- * Carriage Return (0Dh) causes end-of-line
- * Line Feed (0Ah) ignored
- * Form Feed (0Ch) causes end-of-line and end-of-page
- * Tab Char (09h) expands to TAB_SIZE blanks
- * All other codes treated as text.
- *
- * (5) To handle files with ASCII values above 127
- * the program should be compiled with default
- * char type of unsigned (/J on MSC, -K on TurboC).
- */
-
- #define MAX_LEN 80 /* File Names Buffer Length */
- #define HDR_HGT 18 /* Page header space in points */
- #define FTR_HGT 18 /* Page footer space in points */
- #define LINE_SIZE 132 /* I/O buffer length */
- #define TAB_SIZE 5 /* number of blanks per tab */
-
- /* special characters */
- #define TAB_CH 9
- #define FF_CH 12
- #define CR_CH 13
- #define LF_CH 10
-
- /* macro to test for end-of-line */
- #define eoln( c ) ( ((c==CR_CH) || (c==FF_CH) || (c==0) ) ? 1 : 0 )
-
- /* macro to test for end-of-page */
- #define eopg( c, n ) ( ((c==FF_CH) || (n>=LnPerPg)) ? 1 : 0 )
-
- /* if not using RIPS Image 4000, undefine RIPS */
- #define RIPS 1
-
- char FileToPrint[MAX_LEN+1] = "", /* file to be printed */
- PrintDevice[MAX_LEN+1] = "LPT2", /* printer port */
- DateAndTime[28], /* time and date */
- FontName[MAX_LEN+1] = "Courier"; /* font to use */
-
- int PtSize = 11, /* text hgt in points */
- LnSize = 12, /* line hgt in points */
- LnPerPg = 0, /* lines per page */
- LineNo = 0, /* current line number */
- PageNo = 0; /* current page number */
-
- int XLft, XRgt, XCur, /* page coordinate variables */
- YTop, YBot, YCur;
-
- time_t currentT; /* current time */
-
- FILE *fTxt, /* text file in */
- *fPrt; /* printer out */
-
- /*-----------------------------------------------------------------*/
-
- /* display a message and exit */
-
- void exit_pgm( char *msg, int retv )
- {
- printf( "\n%s", msg );
- exit( retv );
- }
-
- /*-----------------------------------------------------------------*/
-
- /* explain program usage */
-
- void explain_pgm( void )
- {
- printf( "\n" );
- printf( "\nPSP - Program to print text files on PostScript printers" );
- printf( "\n" );
- printf( "\nusage: PSP file_to_print [printer_port]" );
- printf( "\n file_to_print : path to an ASCII text file" );
- printf( "\n printer_port : path to printer [def=LPT2]" );
- printf( "\n" );
- }
-
- /*-----------------------------------------------------------------*/
-
- /* initialize for processing */
-
- void start_up( char *f_in, char *f_out )
- {
- int i;
-
- fTxt = fopen( f_in, "rt" );
- if( fTxt == NULL ) exit_pgm( "Could not locate input file", 8 );
-
- fPrt = fopen( f_out, "wt" );
- if( fPrt == NULL ) exit_pgm( "Error opening printer", 8 );
-
- time( ¤tT );
- strcpy( DateAndTime, ctime( ¤tT ) );
- /* get rid of trailing newline character */
- i = 0;
- while( DateAndTime[i] >= ' ' ) i++;
- DateAndTime[i] = 0;
- }
-
- /*-----------------------------------------------------------------*/
-
- /* clean up before returning to DOS */
-
- void shut_dn( void )
- {
- fclose( fTxt );
- fclose( fPrt );
- printf( "\nTotal page(s) printed = %d", PageNo );
- }
-
- /*-----------------------------------------------------------------*/
-
- /* function to write font-select code */
-
- void set_font( void )
- {
- fprintf( fPrt, "\n/%s findfont", FontName );
- fprintf( fPrt, "\n%d scalefont", PtSize );
- fprintf( fPrt, "\nsetfont" );
- }
-
- /*-----------------------------------------------------------------*/
-
- /* function to draw a line between passed endpoints */
-
- void draw_ln( int x1, int y1, int x2, int y2 )
- {
- fprintf( fPrt, "\n%d %d moveto %d %d lineto stroke",
- x1, y1, x2, y2 );
- }
-
- /*-----------------------------------------------------------------*/
-
- /* function to output a string with surrounding parentheses */
- /* special characters are also "escaped" by prefixing with '\' */
-
- void write_str( char *str )
- {
- fputc('\n', fPrt );
- fputc('(', fPrt );
- while( *str )
- {
- if( (*str=='(') || (*str==')') || (*str=='\\') )
- fputc( '\\', fPrt );
- fputc( *str, fPrt );
- str++;
- }
- fputc(')', fPrt );
- }
-
- /*-----------------------------------------------------------------*/
-
- /* function to draw a right justified string at (x,y) */
-
- void rjust_str( char *str, int x, int y )
- {
- write_str( str );
- fprintf( fPrt,
- "\ndup stringwidth pop %d exch sub %d moveto show",
- x, y );
- }
-
- /*-----------------------------------------------------------------*/
-
- /* function to draw a left justified string at (x,y) */
-
- void ljust_str( char *str, int x, int y )
- {
- write_str( str );
- fprintf( fPrt, "\n%d %d moveto show", x, y );
- }
-
- /*-----------------------------------------------------------------*/
-
- /* function to draw a center justified string at (x,y) */
-
- void cjust_str( char *str, int x, int y )
- {
- write_str( str );
- fprintf( fPrt,
- "\ndup stringwidth pop 2 idiv %d exch sub %d moveto show",
- x, y );
- }
-
- /*-----------------------------------------------------------------*/
-
- /* start a new page of text */
-
- void start_pg( void )
- {
- /* file name at upper left */
- ljust_str( FileToPrint, XLft, YTop-PtSize );
-
- /* today's date at upper right */
- rjust_str( DateAndTime, XRgt, YTop-PtSize );
-
- /* line across top */
- draw_ln( XLft, YTop-LnSize-2, XRgt, YTop-LnSize-2 );
-
- /* set current position */
- XCur = XLft;
- YCur = YTop - 18 - PtSize;
- }
-
- /*-----------------------------------------------------------------*/
-
- /* finish up current page */
-
- void finish_pg( void )
- {
- char pg_lbl[16];
-
- /* line across bottom */
- draw_ln( XLft, YBot+LnSize, XRgt, YBot+LnSize );
-
- /* page number at bottom center */
- sprintf( pg_lbl, "- %d -", ++PageNo );
- cjust_str( pg_lbl, (XRgt+XLft)/2, YBot );
-
- /* print the page */
- fprintf( fPrt, "\nshowpage\n" );
- }
-
- /*-----------------------------------------------------------------*/
-
- /* main process loop */
-
- void do_print( void )
- {
- int ic, oc, i;
- char cur_iline[LINE_SIZE+1],
- cur_oline[LINE_SIZE+1];
-
- /* initialize page coord variables */
- XLft = 54; XRgt = 558;
- YTop = 738; YBot = 54;
-
- /* how many lines per page ? */
- LnPerPg = (YTop - YBot - HDR_HGT - FTR_HGT) / LnSize;
-
- /* print a comment as the first line out */
- fprintf( fPrt, "%s", "%" );
- fprintf( fPrt, "\n%s PSP : printing '%s' on %s",
- "%", FileToPrint, DateAndTime );
- fprintf( fPrt, "\n%s", "%" );
-
- /* write code to select a font */
- set_font();
-
- /* "prime" the loop */
- cur_iline[LINE_SIZE] = 0;
- LineNo = 0;
- fgets( cur_iline, LINE_SIZE, fTxt );
-
- /* process each line in the file */
- while( !feof( fTxt ) )
- {
- /* start of a new page? */
- if( LineNo == 0 )
- start_pg();
-
- ic = oc = 0;
- while( !eoln( cur_iline[ic] ) )
- {
- /* expand a tab character */
- if( cur_iline[ic] == TAB_CH )
- for( i=0; i<TAB_SIZE; i++ )
- cur_oline[oc++] = ' ';
- /* ignore line feeds */
- else if( cur_iline[ic] != LF_CH )
- cur_oline[oc++] = cur_iline[ic];
- ic++;
- }
- cur_oline[oc] = 0;
-
- /* print the line */
- if( oc > 0 )
- ljust_str( cur_oline, XCur, YCur );
-
- /* update counters, positions */
- YCur -= LnSize;
- LineNo++;
-
- /* current page full? */
- if( eopg( cur_iline[ic], LineNo ) )
- {
- finish_pg();
- LineNo = 0;
- }
-
- /* get another line */
- fgets( cur_iline, LINE_SIZE, fTxt );
- }
-
- /* finish any partial page at the end */
- if( LineNo > 0 )
- finish_pg();
-
- #ifdef RIPS
- fprintf( fPrt, "\004" );
- #endif
- }
-
- /*-----------------------------------------------------------------*/
-
- /* main... */
-
- void main( int argc, char *argv[] )
- {
- /* check command line */
- if( (argc < 2) || (*argv[1] == '?') )
- {
- explain_pgm();
- exit_pgm( "", 0 );
- }
-
- /* identify ourselves */
- printf( "\nPSP - PostScript File Printing Program" );
-
- /* set up... */
- strcpy( FileToPrint, argv[1] );
- strupr( FileToPrint );
- if( argc > 2 ) strcpy( PrintDevice, argv[2] );
- start_up( FileToPrint, PrintDevice );
-
- /* print the file */
- printf( "\nPrinting '%s' to '%s'...", FileToPrint, PrintDevice );
- do_print();
-
- /* clean up and exit */
- shut_dn();
- exit_pgm( "End of Pgm", 0 );
- }
-